<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-collapse v-model="activeKey">
            <a-collapse-panel key="1" header="日志记录">
        <a-row :gutter="48">
          <a-col :span="12">
            <a-form-model-item label="单据类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="erpType">
              <j-dict-select-tag :disabled=formDisabled type="list" v-model="model.erpType" dictCode="kingdee_bill_type" placeholder="请选择单据类型"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单据编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="billNo">
              <a-input :disabled=formDisabled v-model="model.billNo" :max-length="36"  placeholder="请输入单据编号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单据id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="erpId">
              <a-input :disabled=formDisabled v-model="model.erpId" :max-length="36"  placeholder="请输入单据id"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="服务" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="service">
              <a-input :disabled=formDisabled v-model="model.service" :max-length="36"  placeholder="请输入服务"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="是否成功" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isSuccess">
              <j-dict-select-tag :disabled=formDisabled type="list" v-model="model.isSuccess" dictCode="async_status" placeholder="请选择是否成功"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="反馈信息" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="errorMsg">
              <a-input :disabled=formDisabled v-model="model.errorMsg" :max-length="36"  placeholder="请输入反馈信息"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-collapse-panel>
      <a-collapse-panel key="2" header="付款单">
        <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="paymentCol"
        :dataSource="paymentDataSource"
        class="j-table-force-nowrap"
        :customRow="customPaymentRow">
        <span slot="action" slot-scope="text, record">
        <template v-if="record.isSync=='0'">
          <a-button  type="primary" size="small" @click="handlePaymentAsync(record)"  >同步单据</a-button>
        </template>
        <template v-if="record.isSync=='1'">
          <a-button  type="primary" size="small" @click="handlePaymentAsyncData(record)">更新单据</a-button>
        </template>
        </span>
      </a-table>
      </a-collapse-panel>
      <a-collapse-panel key="3" header="其他应付单">
        <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="defColumns"
        :dataSource="payableDataSource"
        class="j-table-force-nowrap"
        :customRow="customPayableRow">
        <template slot="status" slot-scope="status">
           <a-badge :status="status | statusTypeFilter" :text="status | statusFilter"/>
        </template>

        <span slot="action" slot-scope="text, record">
         
        <template v-if="record.isSync=='0'">
          <a-button  type="primary" size="small" @click="handlePayableAsync(record)" >同步单据</a-button>
        </template>

        <template v-if="record.isSync=='1'">
          <a-button  type="primary" size="small" @click="handlePayableAsyncData(record)">更新单据</a-button>
        </template>
        </span>

      </a-table>
      </a-collapse-panel>
        </a-collapse>
      </a-form-model>
      <erp-finance-kingdee-other-payable-bill-modal ref="payable" ></erp-finance-kingdee-other-payable-bill-modal>
      <erp-finance-kingdee-payment-bill-modal ref="payment" ></erp-finance-kingdee-payment-bill-modal>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import moment from 'moment'
  import { mapGetters } from 'vuex'

  export default {
    name: 'ErpKingdeeAsyncLogForm',
    components: {
      ErpFinanceKingdeeOtherPayableBillModal: () => import('@/views/erp/finance/kingdee/payable/modules/ErpFinanceKingdeeOtherPayableBillModal'),
      ErpFinanceKingdeePaymentBillModal: () => import('@/views/erp/finance/kingdee/payment/modules/ErpFinanceKingdeePaymentBillModal')
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        activeKey:[1,2,3],
        model:{
         },
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          payableList: "/api/erp/finance/kingdee/payable/erpFinanceKingdeeOtherPayableBill/list",
          paymentList: "/api/erp/finance/kingdee/erpFinanceKingdeePaymentBill/list",
          list: "/api/erp/finance/kingdee/payable/erpFinanceKingdeeOtherPayableBill/list",
          add: "/api/erp/finance/kingdee/log/erpKingdeeAsyncLog/add",
          edit: "/api/erp/finance/kingdee/log/erpKingdeeAsyncLog/edit",
          queryById: "/api/erp/finance/kingdee/log/erpKingdeeAsyncLog/queryById",
          asyncPayableKingdee: "/api/erp/finance/kingdee/payable/erpFinanceKingdeeOtherPayableBill/asyncKingdee",
          asyncPayableKingdeeData: "/api/erp/finance/kingdee/payable/erpFinanceKingdeeOtherPayableBill/asyncKingdeeData",
          asyncPaymentKingdee: "/api/erp/finance/kingdee/erpFinanceKingdeePaymentBill/asyncKingdee",
          asyncPaymentKingdeeData: "/api/erp/finance/kingdee/erpFinanceKingdeePaymentBill/asyncKingdeeData"
        },
        paymentDataSource: [],
        payableDataSource: [],
        paymentCol:[
          {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'单据类型',
            align:"center",
            dataIndex: 'fbilltypeiddata_dictText',
            width:150,
            ellipsis: true
          },
          {
            title:'单据编号',
            align:"center",
            dataIndex: 'fbillno',
            scopedSlots: { customRender: 'fbillno' },
            width:150,
            ellipsis: true
          },
          {
            title:'付款申请人',
            align:"center",
            dataIndex: 'fxyhgbase_dictText',
          },
          {
            title:'业务日期',
            align:"center",
            dataIndex: 'fdate',
            width:150,
            ellipsis: true
          },
          // {
          //   title:'审核人',
          //   align:"center",
          //   dataIndex: 'fapproveriddata_dictText',
          // },
          // {
          //   title:'创建日期',
          //   align:"center",
          //   dataIndex: 'fcreatedate',
          //   ellipsis: true
          // },
          {
            title:'结算组织',
            align:"center",
            dataIndex: 'fsettleorgiddata_dictText',
            width:200,
            ellipsis: true
          },
          // {
          //   title:'付款组织',
          //   align:"center",
          //   dataIndex: 'fpayorgiddata_dictText',
          //   ellipsis: true
          // },
          // {
          //   title:'审核日期',
          //   align:"center",
          //   dataIndex: 'fapprovedate',
          // },
          // {
          //   title:'采购组织',
          //   align:"center",
          //   dataIndex: 'fpurchaseorgiddata_dictText',
          // },
          {
            title:'应付金额',
            align:"center",
            dataIndex: 'fpaytotalamountforH',
          },
          {
            title:'实付金额',
            align:"center",
            dataIndex: 'frealpayamountforH',
          },
          // {
          //   title:'币别',
          //   align:"center",
          //   dataIndex: 'fcurrencyid',
          // },
          {
            title:'往来单位类型',
            align:"center",
            width:100,
            dataIndex: 'fcontactunittype_dictText',
          },
          {
            title:'往来单位',
            align:"center",
            dataIndex: 'fcontactunitdata_dictText',
            ellipsis: true
          },
          {
            title:'收款单位类型',
            align:"center",
            width:100,
            dataIndex: 'frectunittype_dictText',
          },
          {
            title:'收款单位',
            align:"center",
            dataIndex: 'frectunitdata_dictText',
            ellipsis: true
          },
          // {
          //   title:'业务类型',
          //   align:"center",
          //   dataIndex: 'fbusinesstype',
          // },
          // {
          //   title:'是否期初单据',
          //   align:"center",
          //   dataIndex: 'fisinit',
          // },
         
          // {
          //   title:'本位币',
          //   align:"center",
          //   dataIndex: 'fmainbookid',
          // },
          // {
          //   title:'结算币别',
          //   align:"center",
          //   dataIndex: 'fsettlecur',
          // },
          // {
          //   title:'应付金额本位币',
          //   align:"center",
          //   dataIndex: 'fpaytotalamountH',
          // },
          // {
          //   title:'付款金额本位币',
          //   align:"center",
          //   dataIndex: 'fpayamount',
          // },
          // {
          //   title:'实付金额本位币',
          //   align:"center",
          //   dataIndex: 'frealpayamountH',
          // },
          // {
          //   title:'是否其他预付',
          //   align:"center",
          //   dataIndex: 'fisborrow',
          // },
          // {
          //   title:'erp单据id',
          //   align:"center",
          //   dataIndex: 'erpBillId',
          // },
          // {
          //   title:'erp单据类型',
          //   align:"center",
          //   dataIndex: 'erpBillType',
          // },
          // {
          //   title:'项目',
          //   align:"center",
          //   dataIndex: 'fqaspassistant1_dictText',
          // },
          {
            title:'备注',
            align:"center",
            dataIndex: 'fremark',
            ellipsis: true
          },
          // {
          //   title:'核算业务类型',
          //   align:"center",
          //   dataIndex: 'fqaspAssistant',
          // },
          // {
          //   title:'单据状态',
          //   align:"center",
          //   dataIndex: 'fdocumentstatus',
          // },
          {
            title:'是否同步',
            align:"center",
            dataIndex: 'isSync_dictText',
            width:80,
          },
          {
            title:'支付状态',
            align:"center",
            dataIndex: 'payStatus_dictText',
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:160,
            scopedSlots: {
            filterDropdown: 'filterDropdown',
            filterIcon: 'filterIcon',
            customRender: 'action' }
          }
        ],
        defColumns: [
          {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          // {
          //   title:'实体主键',
          //   align:"center",
          //   dataIndex: 'fid',
          // },
          {
            title:'单据编号',
            align:"center",
            dataIndex: 'fbillno',
            scopedSlots: { customRender: 'fbillno' },
            width:150,
            ellipsis: true
          },
          {
            title:'报销申请人',
            align:"center",
            dataIndex: 'fxyhgbase_dictText',
          },
          // {
          //   title:'单据状态',
          //   align:"center",
          //   dataIndex: 'fdocumentstatus',
          // },
          // {
          //   title:'单据类型',
          //   align:"center",
          //   dataIndex: 'fbilltypeiddata',
          // },
          {
            title:'业务日期',
            align:"center",
            dataIndex: 'fdate',
            width:150,
            ellipsis: true
          },
          // {
          //   title:'到期日',
          //   align:"center",
          //   dataIndex: 'fenddateh',
          //   customRender:function (text) {
          //     return !text?"":(text.length>10?text.substr(0,10):text)
          //   }
          // },
          {
            title:'往来单位类型',
            align:"center",
            dataIndex: 'fcontactunittype_dictText',
          },
          {
            title:'往来单位',
            align:"center",
            dataIndex: 'fcontactunitdata_dictText',
            ellipsis: true
          },
         
          // {
          //   title:'币别',
          //   align:"center",
          //   dataIndex: 'fcurrencyiddata',
          // },
          {
            title:'结算组织',
            align:"center",
            dataIndex: 'fsettleorgiddata_dictText',
            ellipsis: true
          },
          {
            title:'结算方式',
            align:"center",
            dataIndex: 'fsettletypeiddata_dictText',
            ellipsis: true
          },
          // {
          //   title:'本位币',
          //   align:"center",
          //   dataIndex: 'fmainbookstdcurriddata',
          // },
          // {
          //   title:'汇率类型',
          //   align:"center",
          //   dataIndex: 'fexchangetypedata',
          // },
          // {
          //   title:'汇率',
          //   align:"center",
          //   dataIndex: 'fexchangerate',
          // },
          // {
          //   title:'作废状态',
          //   align:"center",
          //   dataIndex: 'fcancelstatus',
          // },
          // {
          //   title:'未借款金额',
          //   align:"center",
          //   dataIndex: 'fnotsettleamountfor',
          // },
          {
            title:'总金额',
            align:"center",
            dataIndex: 'ftotalamountforh',
          },
          // {
          //   title:'已付款核销金额',
          //   align:"center",
          //   dataIndex: 'fwrittenoffamountfor',
          // },
          // {
          //   title:'是否期初单据',
          //   align:"center",
          //   dataIndex: 'fisinit',
          // },
          // {
          //   title:'不含税金额本位币',
          //   align:"center",
          //   dataIndex: 'fnotaxamount',
          // },
          // {
          //   title:'税额本位币',
          //   align:"center",
          //   dataIndex: 'ftaxamount',
          // },
          // {
          //   title:'到期日计算日期',
          //   align:"center",
          //   dataIndex: 'faccnttimejudgetime',
          //   customRender:function (text) {
          //     return !text?"":(text.length>10?text.substr(0,10):text)
          //   }
          // },
          // {
          //   title:'付款组织',
          //   align:"center",
          //   dataIndex: 'fpayorgiddata_dictText',
          // },
          // {
          //   title:'业务类型',
          //   align:"center",
          //   dataIndex: 'fbusinesstype',
          // },
          {
            title:'报销类型',
            align:"center",
            dataIndex: 'fxyhgassistant2_dictText',
          },
          {
            title:'备注',
            align:"center",
            dataIndex: 'fremarks',
            ellipsis: true
          },
          // {
          //   title:'项目',
          //   align:"center",
          //   dataIndex: 'fxyhgassistant_dictText',
          // },
          {
            title:'是否同步',
            align:"center",
            dataIndex: 'isSync_dictText',
            width:80,
          },
          // {
          //   title:'erp单据类型',
          //   align:"center",
          //   dataIndex: 'erpBillType',
          // },
          // {
          //   title:'erp单据id',
          //   align:"center",
          //   dataIndex: 'erpBillId',
          // },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:160,
            scopedSlots: {
            filterDropdown: 'filterDropdown',
            filterIcon: 'filterIcon',
            customRender: 'action' }
          }
        ],
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      handlePaymentAsyncData(record){
        let params=null;
        if(record){
          params={ids:record.id}
        }
        getAction(this.url.asyncPaymentKingdeeData,params).then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.loadPaymentList(this.model);
            this.loadPayableList(this.model);
          }else{
            this.$message.error(res.message)
          }
        })
      },
      handlePaymentAsync(record){
        let params=null;
        if(record){
          params={ids:record.id}
        }
        getAction(this.url.asyncPaymentKingdee, params).then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.loadPaymentList(this.model);
            this.loadPayableList(this.model);
          }else{
            this.$message.error(res.message)
          }
        })
      },
      handlePayableAsyncData(record){
        let params=null;
        if(record){
          params={ids:record.id}
        }
        getAction(this.url.asyncPayableKingdeeData,params).then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.loadPaymentList(this.model);
            this.loadPayableList(this.model);
          }else{
            this.$message.error(res.message)
          }
        })
      },
      handlePayableAsync(record){
        let params=null;
        if(record){
          params={ids:record.id}
        }
        getAction(this.url.asyncPayableKingdee, params).then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.loadPaymentList(this.model);
            this.loadPayableList(this.model);
          }else{
            this.$message.error(res.message)
          }
        })
      },
      // table 双击查看详情
    customPaymentRow(record) {
      return {
        on: {
          // 事件
          dblclick: event => {
            this.handlePaymentDetail(record)
          }
        }
      }
    },
      handlePaymentDetail: function (record) {
        this.$refs.payment.edit(record);
        this.$refs.payment.title = '详情';
        this.$refs.payment.disableSubmit = true;
      },
       // table 双击查看详情
    customPayableRow(record) {
      return {
        on: {
          // 事件
          dblclick: event => {
            this.handlePayableDetail(record)
          }
        }
      }
    },
      handlePayableDetail: function (record) {
        this.$refs.payable.edit(record);
        this.$refs.payable.title = '详情';
        this.$refs.payable.disableSubmit = true;
      },
      loadPayableList(record){
        getAction(this.url.payableList,{"erpBillId":record.erpId,"erpBillType":record.erpType}).then((res) => {
          if (res.success) {
            let result=res.result.records
            if(result.length>0){
              this.payableDataSource=result;
            }
          }else{
            this.$message.error(res.message)
          }
        })
      },
      loadPaymentList(record){
        getAction(this.url.paymentList,{"erpBillId":record.erpId,"erpBillType":record.erpType}).then((res) => {
          if (res.success) {
            let result=res.result.records
            if(result.length>0){
             this.paymentDataSource=result
            }
          }else{
            this.$message.error(res.message)
          }
        })
      },
      ...mapGetters(['userInfo']),
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.$refs.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.loadPaymentList(this.model);
        this.loadPayableList(this.model);
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
                that.$emit('unButtonDisable');
              }
            }).catch((error)=>{
               that.$emit('unButtonDisable');
             }).finally(() => {
               that.confirmLoading = false;
             })
          }else {
           that.$emit('unButtonDisable');
          }

        })
      },
    }
  }
</script>
